package com.lvhx.springboot.dynamicdatasource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;


/**
 * Mybatis配置
 * @author Louis
 * @date Oct 31, 2018
 */
//@Configuration
public class MybatisConfig {

//    @Bean("master")
//    @Primary
//    @ConfigurationProperties(prefix = "spring.datasource.master")
//    public DataSource master() {
//        return DataSourceBuilder.create().build();
//    }
//
//    @Bean("slave")
//    @ConfigurationProperties(prefix = "spring.datasource.slave")
//    public DataSource slave() {
//        return DataSourceBuilder.create().build();
//    }
//
//    @Bean("dynamicDataSource")
//    public DataSource dynamicDataSource() {
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        Map<Object, Object> dataSourceMap = new HashMap<>(2);
//        dataSourceMap.put("master", master());
//        dataSourceMap.put("slave", slave());
//        // 将 master 数据源作为默认指定的数据源
//        dynamicDataSource.setDefaultDataSource(master());
//        // 将 master 和 slave 数据源作为指定的数据源
//        dynamicDataSource.setDataSources(dataSourceMap);
//        return dynamicDataSource;
//    }
//
//    @Bean("sqlSessionFactoryBean")
//    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
//        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        // 配置数据源，此处配置为关键配置，如果没有将 dynamicDataSource作为数据源则不能实现切换
////        sessionFactory.setDataSource(dynamicDataSource());
//        sessionFactory.setDataSource(master());
////        sessionFactory.setTypeAliasesPackage("com.lvhx.springboot.entity");    // 扫描Model
////        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
////        sessionFactory.setMapperLocations(resolver.getResources("classpath*:*Mapper.xml"));    // 扫描映射文件
//        return sessionFactory;
//    }
//
//    @Bean("jobExplorer")
//    public JobExplorerFactoryBean jobExplorer() throws Exception{
//        JobExplorerFactoryBean jobExplorer = new JobExplorerFactoryBean();
//        jobExplorer.setDataSource(master());
//        return jobExplorer;
//    }
//
//    @Bean("transactionManager")
//    public PlatformTransactionManager transactionManager() {
//        // 配置事务管理, 使用事务时在方法头部添加@Transactional注解即可
//        return new DataSourceTransactionManager(dynamicDataSource());
//    }
}